package tt.dz.service.imp.charger;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.charger.OmsChargerRateDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.charger.OmsChargerRate;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.charger.ChargerRateService;
import tt.dz.service.imp.base.BaseService;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ChargerRateServiceImpl extends BaseService implements ChargerRateService
{
	@Autowired
	private OmsChargerRateDao omsChargerRateDaoImpl;
	@Override
	public  synchronized JsonCommon<?> add(List<OmsChargerRate> objs,Integer rateSchema) 
	{
		try 
		{
			Integer maxRateSchema =  rateSchema;
			if (rateSchema != null)
			{
				omsChargerRateDaoImpl.deleteChargerRatesByRateSchema(rateSchema);
			}
			else
			{
				maxRateSchema = omsChargerRateDaoImpl.findMaxRateSchema() + 1;
			}
			omsChargerRateDaoImpl.batchAddChargerRatesByRateSchema(objs,maxRateSchema);
			
		}
		catch (OmsDaoException e) 
		{
			this.logError("chargerRate Service add", e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon(objs);
	}
	
	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsChargerRate obj) 
	{
		try 
		{
			List<OmsChargerRate> lst = omsChargerRateDaoImpl.pagingFind(param, obj);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsChargerRateDaoImpl.queryTotalByObject(obj);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonCommon<?> deleteLogic(String rateSchema, String serivalNo) 
	{
		try 
		{
			if (StringUtils.isNumeric(rateSchema) && StringUtils.isNumeric(serivalNo))
			{
				OmsChargerRate obj = new OmsChargerRate();
				obj.setRate_schema(Integer.parseInt(rateSchema));
				obj.setSerial_no(Integer.parseInt(serivalNo));
				omsChargerRateDaoImpl.LogicDeleteObject(obj);
			}
			
		}
		catch (Exception e) 
		{
			this.logError("chargerRate Service delete", e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon("success");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<OmsChargerRate> findExample(OmsChargerRate omsChargerRate){
		try 
		{
			List  lst = omsChargerRateDaoImpl.findExample(omsChargerRate, null, null, "serial_no", 1);
			return lst;
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return null;
		}
	}
	
}
